package com.example.sunny.view.map

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import com.baidu.location.LocationClient
import com.baidu.mapapi.map.*
import com.example.sunny.R
import com.example.sunny.SunnyApplication
import com.example.sunny.pojo.location.MyLocation
import com.example.sunny.service.network.location.LocationService
import com.example.sunny.utils.NetUtils
import kotlinx.android.synthetic.main.activity_map_view_acitivity.*

/**
 * 地图
 */
class MapViewActivity: AppCompatActivity(), LocationService.MyLocationListener.LocationCallback{
    lateinit var baiduMap: BaiduMap

    lateinit var locationClient: LocationClient

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        val option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        val vis: Int = window.decorView.systemUiVisibility
        window.decorView.systemUiVisibility = option or vis
        window.statusBarColor = Color.TRANSPARENT
        setContentView(R.layout.activity_map_view_acitivity)
        //获取地图
        baiduMap = mapView.map
        baiduMap.isMyLocationEnabled =true
        //设置地图放大倍数
        init()
        //设置地图定位参数
        config()

        val myLocation = intent.getParcelableExtra<MyLocation>("my_location")

        myLocation?.let {
            val data = MyLocationData.Builder()
                .accuracy(it.accuracy)
                .direction(it.direction)
                .latitude(it.lat)
                .longitude(it.lng)
                .build()
            baiduMap.setMyLocationData(data)
        }


    }

    private fun init(){
        val builder = MapStatus.Builder()
        builder.zoom (15f)
        baiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))

    }

    private fun config(){
        baiduMap.setMyLocationConfiguration(MyLocationConfiguration
            (MyLocationConfiguration.LocationMode.FOLLOWING,true,
            BitmapDescriptorFactory.fromResource(R.drawable.ic_locate),
            0xAAFFFF88.toInt(), 0xAA00FF00.toInt()
        ))
    }



    //定位
    override fun onResume() {
        mapView.onResume()
        super.onResume()
    }

    override fun onPause() {
        mapView.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        baiduMap.isMyLocationEnabled = false
        mapView.onDestroy()
        super.onDestroy()
    }

    override fun getLocation(myLocation: MyLocation) {
        val data = MyLocationData.Builder()
                .accuracy(myLocation.accuracy)
                .direction(myLocation.direction)
                .latitude(myLocation.lat)
                .longitude(myLocation.lng)
                .build()
        baiduMap.setMyLocationData(data)
    }

}